import Settings from '../config/Settings.js';

export default class Crow {
  constructor(game, options = {}) {
    this.game = game;
    this.x = options.x || game.canvas.width;
    this.y = options.y || game.canvas.height / 2;
    
    const config = Settings.enemies.types.crow;
    this.width = config.width;
    this.height = config.height;
    this.speed = config.speed;
    this.damage = config.damage;
    
    // 动画相关
    this.sprite = config.sprite;
    this.currentFrame = 0;
    this.frameTimer = 0;
    this.currentAnimation = 'fly';
    this.image = game.getImage('crow');
    
    // 移动相关
    this.targetY = this.y;
    this.amplitude = 80;  // 飞行波动幅度
    this.frequency = 0.02; // 飞行波动频率
    this.initialY = this.y;
    this.moveTimer = 0;
    
    // 攻击相关
    this.isAttacking = false;
    this.attackTimer = 0;
    this.attackCooldown = 120; // 攻击冷却时间
    this.attackRange = 200;   // 攻击触发距离
  }
  
  update() {
    // 更新动画
    const animation = this.sprite.animations[this.currentAnimation];
    this.frameTimer += animation.speed;
    
    if (this.frameTimer >= 1) {
      this.frameTimer = 0;
      this.currentFrame++;
      if (this.currentFrame > animation.endFrame) {
        this.currentFrame = animation.startFrame;
      }
    }
    
    // 基础移动
    this.x -= this.speed;
    
    // 波浪形飞行
    if (!this.isAttacking) {
      this.moveTimer += this.frequency;
      this.y = this.initialY + Math.sin(this.moveTimer) * this.amplitude;
      
      // 检查是否需要攻击
      const player = this.game.player;
      const distanceX = this.x - player.x;
      const distanceY = Math.abs(this.y - player.y);
      
      if (distanceX < this.attackRange && distanceY < 100 && this.attackTimer <= 0) {
        this.startAttack();
      }
    }
    
    // 攻击状态更新
    if (this.isAttacking) {
      this.updateAttack();
    }
    
    // 更新攻击冷却
    if (this.attackTimer > 0) {
      this.attackTimer--;
    }
  }
  
  startAttack() {
    this.isAttacking = true;
    this.currentAnimation = 'attack';
    this.frameTimer = 0;
    this.currentFrame = this.sprite.animations.attack.startFrame;
    
    // 记录攻击目标位置
    this.targetY = this.game.player.y;
    this.attackStartX = this.x;
    this.attackStartY = this.y;
    this.attackProgress = 0;
  }
  
  updateAttack() {
    this.attackProgress += 0.05;
    
    if (this.attackProgress >= 1) {
      // 攻击结束
      this.isAttacking = false;
      this.currentAnimation = 'fly';
      this.attackTimer = this.attackCooldown;
      return;
    }
    
    // 使用二次贝塞尔曲线计算攻击路径
    const targetX = this.attackStartX - 200;
    const controlX = (this.attackStartX + targetX) / 2;
    const controlY = this.attackStartY - 100;
    
    const t = this.attackProgress;
    const mt = 1 - t;
    
    this.x = mt * mt * this.attackStartX + 2 * mt * t * controlX + t * t * targetX;
    this.y = mt * mt * this.attackStartY + 2 * mt * t * controlY + t * t * this.targetY;
  }
  
  render(ctx) {
    ctx.save();
    
    // 绘制阴影
    ctx.fillStyle = 'rgba(0, 0, 0, 0.2)';
    ctx.beginPath();
    ctx.ellipse(this.x + this.width/2, this.game.canvas.height - 60, 
                this.width/3, this.height/6, 0, 0, Math.PI * 2);
    ctx.fill();
    
    // 计算当前帧在精灵图上的位置
    const frameX = (this.currentFrame % this.sprite.sheet.framesPerRow) * this.sprite.sheet.frameWidth;
    const frameY = Math.floor(this.currentFrame / this.sprite.sheet.framesPerRow) * this.sprite.sheet.frameHeight;
    
    try {
      if (this.image && !this.image.isPlaceholder) {
        ctx.drawImage(
          this.image,
          frameX, frameY,
          this.sprite.sheet.frameWidth,
          this.sprite.sheet.frameHeight,
          this.x, this.y,
          this.width, this.height
        );
      }
    } catch (error) {
      console.error('渲染乌鸦时发生错误:', error);
    }
    
    ctx.restore();
  }
  
  checkCollision(player) {
    // 缩小实际碰撞范围，使游戏更友好
    const hitboxWidth = this.width * 0.7;
    const hitboxHeight = this.height * 0.7;
    const hitboxX = this.x + (this.width - hitboxWidth) / 2;
    const hitboxY = this.y + (this.height - hitboxHeight) / 2;
    
    const collision = (
      player.x + player.width/2 > hitboxX &&
      player.x - player.width/2 < hitboxX + hitboxWidth &&
      player.y < hitboxY + hitboxHeight &&
      player.y + player.height > hitboxY
    );
    
    if (collision) {
      // 如果玩家从上方跳到乌鸦头上
      if (player.velocity > 0 && 
          player.y + player.height < this.y + hitboxHeight/2) {
        return 'defeat';
      } else {
        return 'damage';
      }
    }
    
    return false;
  }
} 